<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Config;
use think\Db;

/**
 * 控制台
 *
 * @icon fa fa-dashboard
 * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
 */
class Dashboard extends Backend
{

    /**
     * 查看
     */
    public function index()
    {
        $seventtime = \fast\Date::unixtime('day', -7);

        $createlist = [];
        $sql = "SELECT FROM_UNIXTIME(createtime,'%Y%m%d') days,COUNT(id) count FROM fa_v_user_order where createtime > " . $seventtime . " GROUP BY days; ";
        $list = Db::query($sql);
        foreach ($list as $v) {
            $createlist[$v['days']] = $v['count'];
        }

        $hooks = config('addons.hooks');
        $uploadmode = isset($hooks['upload_config_init']) && $hooks['upload_config_init'] ? implode(',', $hooks['upload_config_init']) : 'local';
        $addonComposerCfg = ROOT_PATH . '/vendor/karsonzhang/fastadmin-addons/composer.json';
        Config::parse($addonComposerCfg, "json", "composer");
        $config = Config::get("composer");
        $addonVersion = isset($config['version']) ? $config['version'] : __('Unknown');

        $user_db = Db::name("user");
        $totaluser = $user_db->where("username != 'admin'")->count();

        $course_db = Db::name('v_course');
        $course_count = $course_db->count();

        $v_user_order_db = Db::name('v_user_order');
        $totalorder = $v_user_order_db->where("state=1")->count();
        $totalorderamount = $v_user_order_db->where("state=1")->sum("money");

//        $course_db = Db::name('v_course');
//        $course_count = $course_db->count();
        //今日注册
        $todayuserlogin = $user_db->where("createtime", 'gt', strtotime(date("Y-m-d", time())))->count();
        //今日登陆
        $todayusersignup = $user_db->where("logintime", 'gt', strtotime(date("Y-m-d", time())))->count();
        //今日订单
        $todayorder = $v_user_order_db->where("state=1")
            ->where("createtime", 'gt', strtotime(date("Y-m-d", time())))
            ->count();
        //今日意见
        $suggestday = $v_user_order_db->where("createtime", 'gt', strtotime(date("Y-m-d", time())))
            ->count();
        //七日增长
        $todayusercreate = $user_db->where("createtime", 'gt', strtotime(date("Y-m-d", strtotime('-7 day'))))->count();
        //七日活跃
        $todayuserloginseven = $user_db->where("logintime", 'gt', strtotime(date("Y-m-d", strtotime('-7 day'))))->count();

        $sevendnu = ceil($todayusercreate / $totaluser * 100) . '%';
        $sevendau = ceil($todayuserloginseven / $totaluser * 100) . '%';
        $this->view->assign([
            'totaluser' => $totaluser,
            'totalviews' => $course_count,
            'totalorder' => $totalorder,
            'totalorderamount' => $totalorderamount,
            'todayuserlogin' => $todayuserlogin,
            'todayusersignup' => $todayusersignup,
            'todayorder' => $todayorder,
            'unsettleorder' => $suggestday,
            'sevendnu' => $sevendnu,
            'sevendau' => $sevendau,
            'createlist' => $createlist,
            'addonversion' => $addonVersion,
            'uploadmode' => $uploadmode
        ]);

        return $this->view->fetch();
    }

}
